import request from '@/utils/request'
import { LOGIN_TYPE, LOGOUT_TYPE } from '@/redux/constant';

export const login = function (payload) {
    return {
        type: LOGIN_TYPE,
        payload
    }
}
export const logout = function () {
    return {
        type: LOGOUT_TYPE,
    }
}
//获取用户信息
export function getInfo(props) {
    return async function (dispatch) {
        const { data } = await request.get('/user/info');
        if (!data.success) {
            props.history.push('/login')
            return;
        }
        //储存用户信息
        dispatch(login(data.data))
        return data;
    }
}
//修改信息
export function updateInfo(userinfo) {
    return async function (dispatch) {
        const { data1 } = await request.post('/user/updateInfo', { ...userinfo });
        const { data } = await request.get('/user/info');
        dispatch(login(data.data))
        return data;
    }
}